<template>
  <div class="">
    <van-nav-bar
      title="修改昵称"
      left-text="取消"
      right-text="完成"
      left-arrow
      @click-left="$emit('close')"
      @click-right="onClickRight"
    />
    <!-- 文本 输入框 -->
    <van-cell-group>
      <van-field
        v-model.trim="message"
        rows="2"
        autosize
        type="textarea"
        maxlength="7"
        placeholder="请输入新昵称"
        show-word-limit
      />
    </van-cell-group>
  </div>
</template>

<script>
import { updateUserName } from '@/api/user'
export default {
  name: 'UpdateName',
  components: {

  },
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      message: this.value
    }
  },
  created () {

  },
  methods: {
    // 点击 完成 触发事件
    async onClickRight () {
      this.$toast.loading({
        message: '保存中...',
        forbidClick: true,
        duration: 0
      })
      try {
        if (this.message.length === 0) {
          return
        }
        await updateUserName({
          name: this.message
        })
        // 修改父组件的昵称
        this.$emit('input', this.message)
        // 关闭弹出层
        this.$emit('close')
        this.$toast('修改成功！')
      } catch (e) {
        console.error(e)
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
